/**
 * 核心js类
 * 
 * @author wodenwang
 */
var Core = {

	/*
	 * public方法
	 */
	/**
	 * 界面（区域）初始化
	 * 
	 * @param zone
	 *            区域id
	 */
	init : function(zone) {
		var zoneId;
		var seq = 1;// 自动编号，用于生成自动ID。
		if (!zone || zone == '') {
			zone = '';
			zoneId = '_body_id';
			$('body').attr("id", zoneId);
		} else {
			zoneId = zone;
			zone = '#' + zone + ' ';
		}

		// 初始化style重写模块
		$(zone + "table.ws-table").styleTable();
		$(zone + "button").styleButton();
		$(zone + "div.ws-bar").styleBar();
		$(zone + "div.ws-msg.info").styleMsg({
			type : 'info'
		});
		$(zone + "div.ws-msg.warning").styleMsg({
			type : 'warning'
		});
		$(zone + "div.ws-msg.error").styleMsg({
			type : 'error'
		});

		// 初始化tab
		$.each($(zone + 'div[tabs=true]'), function() {
			var $this = $(this);
			var $ul = $("<ul></ul>");
			$.each($this.children('div'), function(index) {
				var $div = $(this);
				var id = $div.attr('id');
				var title = $div.attr('title');
				if (id == null || id == '') {
					id = zoneId + '_' + (seq++);
					$(this).attr('id', id);
				}
				if (title == null || title == '') {
					title = '标签' + id;
				}
				$div.attr("tab", index);
				$ul.append('<li><a tab="' + index + '" href="#' + id + '">'
						+ title + '</a></li>');
			});

			$this.prepend($ul);
			$this.tabs();
		});

		// 初始化div init标签
		$.each($(zone + 'div[init]'), function() {
			$(this).addClass('ws-zone');
			var id = $(this).attr('id');
			if (id == null || id == '') {
				id = zoneId + '_' + (seq++);
				$(this).attr('id', id);
			}
			var url = $(this).attr('init');

			var tabindex = $(this).attr("tab");
			if (tabindex != null && 0 + tabindex > 0) {// 非活动标签则延迟加载
				$('ul li a[tab=' + tabindex + ']', $(this).parent()).click(
						function() {
							var tabloaded = $(this).attr("tabloaded");
							if (tabloaded == 'true') {
								return;
							}

							if (url != null && url != '') {
								Ajax.post(id, url);
							}
							$(this).attr("tabloaded", "true");
						});
			} else {
				if (url != null && url != '') {
					Ajax.post(id, url);
				}
			}
		});

		// 处理排序
		$.each($(zone + 'th[field]'), function() {
			var $this = $(this);
			var field = $this.attr('field');
			var table = $this.parents('table').first();
			var formid = table.attr("form");
			var form;
			if (formid == null || formid == '') {
				formid = zoneId + '_' + (seq++);
				table.attr("form", formid);
				if ($('#' + formid).size() < 1) {
					form = $('<form method="post"></form>');
					form.attr("id", formid);
					form.attr("action", $(zone).attr("init"));
					$(zone).before(form);
				}
			}
			form = $('#' + formid);

			// 插入隐藏field
			if ($('input[name=_field]', form).size() < 1) {
				form.append('<input name="_field" type="hidden"/>');
			}
			// 插入隐藏dir
			if ($('input[name=_dir]', form).size() < 1) {
				form.append('<input name="_dir" type="hidden"/>');
			}

			// 插入隐藏page
			if ($('input[name=_page]', form).size() < 1) {
				form.append('<input name="_page" type="hidden"/>');
			}

			var fieldVal = $('input[name=_field]', $('#' + formid)).val();
			var dirVal = $('input[name=_dir]', $('#' + formid)).val();
			// FIXME . 初始化图标.有点丑，以后优化
			if (field == fieldVal) {
				if (dirVal == 'asc') {
					$this.append('<span>&#8595;</span>');
				} else if (dirVal == 'desc') {
					$this.append('<span>&#8593;</span>');
				}
			}

			// 绑定事件
			$this.click(function(event) {
				var $field = $('input[name=_field]', form);
				var $dir = $('input[name=_dir]', form);
				if ($field.val() != field) {
					$dir.val('desc');
				} else if ($dir.val() == '') {
					$dir.val('desc');
				} else if ($dir.val() == 'desc') {
					$dir.val('asc');
				} else {
					$dir.val('');
				}
				$field.val(field);
				$('input[name=_page]', form).val('1');// 页码强制为1.
				var target = form.attr('zone');
				if (target == null || target == '') {
					target = zoneId;
				}
				Ajax.form(target, form);
			});
			$this.css('cursor', 'pointer');

		});

		// 处理分页
		$.each($(zone + 'div.ws-bar.page'), function() {
			// 处理文字区域，左侧纵向居中
			var $this = $(this);
			$this.find("span").first().position({
				of : $this,
				my : "left middle",
				at : "left middle",
				offset : "10px"
			});

			var formid = $this.attr('form');
			var form;
			if (formid == null || formid == '') {
				formid = zoneId + '_' + (seq++);
				$this.attr("formid", formid);
				if ($('#' + formid).size() < 1) {
					form = $('<form method="post"></form>');
					form.attr("id", formid);
					form.attr("action", $(zone).attr("init"));
					$(zone).before(form);
				}
			}
			form = $('#' + formid);

			// 插入隐藏page
			if ($('input[name=_page]', form).size() < 1) {
				form.append('<input name="_page" type="hidden"/>');
			}

			// 插入隐藏limit
			if ($('input[name=_limit]', form).size() < 1) {
				form.append('<input name="_limit" type="hidden"/>');
			}

			$this.find("button").click(function() {
				var val = $(this).val();
				var $page = $('input[name=_page]', form);
				$page.val(val);
				var target = form.attr('zone');
				if (target == null || target == '') {
					target = zoneId;
				}
				Ajax.form(target, form);
			});
		});

		// 初始化表单
		$.each($(zone + 'form'), function() {
			var target = $(this).attr('zone');
			var sync = $(this).attr('sync');
			if ('true' == sync) {
				// 同步表单则不做ajax处理
				return;
			}
			var $this = $(this);
			if (!target || target == null) {
				target = zoneId;
			}

			// 初始化验证框架
			var valiForm = $this.validate();

			// 綁定事件
			$this.bind('submit', function(event) {
				event.preventDefault();
				if (valiForm.form()) {
					Ajax.form(target, $this);
				}
			});
		});
	},

	/**
	 * 警告框
	 * 
	 * @param msg
	 *            提示信息
	 */
	alert : function(msg) {
		var container = $('#ws-dialog-alert-div');
		if (!container || container.length > 0) {
			container.remove();
		}
		var title = "提示窗口";
		$('body').append(
				'<div id="ws-dialog-alert-div" style="display:none;">' + msg
						+ '</div>');
		$("#ws-dialog-alert-div").styleMsg({
			type : 'info'
		});
		$("#ws-dialog-alert-div").dialog({
			title : title,
			modal : true,
			buttons : {
				'确定' : function() {
					$(this).dialog("close");
				}
			}
		});
	},

	/**
	 * 询问框
	 * 
	 * @param msg
	 *            提示信息
	 * @param fn
	 *            点击OK的回调函数
	 */
	confirm : function(msg, fn) {
		var container = $('#ws-dialog-confirm-div');
		if (!container || container.length > 0) {
			container.remove();
		}
		var title = "提示窗口";
		$('body').append(
				'<div id="ws-dialog-confirm-div" style="display:none;">' + msg
						+ '</div>');
		$("#ws-dialog-confirm-div").styleMsg({
			type : 'warning'
		});
		$("#ws-dialog-confirm-div").dialog({
			title : title,
			modal : true,
			buttons : {
				'确定' : function() {
					$(this).dialog("close");
					fn();
				},
				'取消' : function() {
					$(this).dialog("close");
				}
			}
		});
	}
};
